package com.dkm.ou.me.domain;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.ou.me.persistence.dao.ResDao;
import com.dkm.ou.me.persistence.entity.ResPo;
import com.dkm.ou.me.repository.ResRepository;

/**
 * 对象功能:资源权限 领域对象实体
 * 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston
 * 创建时间:2014-07-21 17:10:20
 */
@Service
@Scope("prototype")
public class Res extends AbstractDomain<String, ResPo>{
	 
	private ResDao resDao = null;
	 
	private ResRepository resRepository;
	 
	protected void init(){
		resDao = SpringHelper.getBean(ResDao.class);
		resRepository = SpringHelper.getBean(ResRepository.class);
		setDao(resDao);
	}	 
	 
	@Override
	public void save() {
		Res parent = null;
		String parentId = getData().getParentId();
		if(StringUtils.isNotEmpty(parentId)){
			parent = resRepository.load(parentId);
		}
		boolean isNew = isNew();
		if(isNew){
			//因为设置了id后，isNew()方法就失效了
			getData().setId(getIdGenerator().genSid());				
		}		
		//设置树结构相关参数：深度、路径
		if(parent==null){						
			getData().setDepth(1);
			getData().setPath(getId());			
		}else{
			getData().setDepth(parent.getData().getDepth()+1);
			getData().setPath(parent.getData().getPath() + "." + getId());
		}		
		
		if(isNew){						
			create();
		}else{			
			update();
		}		
	}	 	
	
}
